package cn.com.surker.util;

import javax.servlet.http.HttpServletRequest;

/**
 * @desc IP获取工具类
 * @date
 */
public class IpUtils {

    /**获取代理过后的IP地址**/
    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("X-requested-For");
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    // 获取ip地址
    public static String getRequestIp(HttpServletRequest request) {
        String requestIp = request.getHeader("x-forwarded-for");

        if (requestIp == null || requestIp.isEmpty() || "unknown".equalsIgnoreCase(requestIp)) {
            requestIp = request.getHeader("X-Real-IP");
        }
        if (requestIp == null || requestIp.isEmpty() || "unknown".equalsIgnoreCase(requestIp)) {
            requestIp = request.getHeader("Proxy-Client-IP");
        }
        if (requestIp == null || requestIp.isEmpty() || "unknown".equalsIgnoreCase(requestIp)) {
            requestIp = request.getHeader("WL-Proxy-Client-IP");
        }
        if (requestIp == null || requestIp.isEmpty() || "unknown".equalsIgnoreCase(requestIp)) {
            requestIp = request.getHeader("HTTP_CLIENT_IP");
        }
        if (requestIp == null || requestIp.isEmpty() || "unknown".equalsIgnoreCase(requestIp)) {
            requestIp = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (requestIp == null || requestIp.isEmpty() || "unknown".equalsIgnoreCase(requestIp)) {
            requestIp = request.getRemoteAddr();
        }
        if (requestIp == null || requestIp.isEmpty() || "unknown".equalsIgnoreCase(requestIp)) {
            requestIp = request.getRemoteHost();
        }

        return requestIp;
    }

}
